import router from '@/sheep/router';
export default {
  /**
   * 打电话
   * @param {String<Number>} phoneNumber - 数字字符串
   */
  callPhone(phoneNumber = '') {
    let num = phoneNumber.toString();
    uni.makePhoneCall({
      phoneNumber: num,
      fail(err) {
        console.log('makePhoneCall出错', err);
      },
    });
  },

  /**
   * 微信头像
   * @param {String} url -图片地址
   */
  checkMPUrl(url) {
    // #ifdef MP
    if (
      url.substring(0, 4) === 'http' &&
      url.substring(0, 5) !== 'https' &&
      url.substring(0, 12) !== 'http://store' &&
      url.substring(0, 10) !== 'http://tmp' &&
      url.substring(0, 10) !== 'http://usr'
    ) {
      url = 'https' + url.substring(4, url.length);
    }
    // #endif
    return url;
  },

  /**
   * getUuid 生成唯一id
   */
  getUuid(len = 32, firstU = true, radix = null) {
    const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    const uuid = [];
    radix = radix || chars.length;

    if (len) {
      // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
      for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
    } else {
      let r;
      // rfc4122标准要求返回的uuid中,某些位为固定的字符
      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
      uuid[14] = '4';

      for (let i = 0; i < 36; i++) {
        if (!uuid[i]) {
          r = 0 | (Math.random() * 16);
          uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
        }
      }
    }
    // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
    if (firstU) {
      uuid.shift();
      return `u${uuid.join('')}`;
    }
    return uuid.join('');
  },
};
const tagEnum = {
	whbk:'/static/img/shop/whbk.png',
	xpss:'/static/img/shop/xpss.png',
	xstj:'/static/img/shop/xstj.png',
	yxhw:'/static/img/shop/yxhw.png',
	zpbz:'/static/img/shop/zpbz.png',
}
const tagEnum1 = {
	hot: {
    img: '/storage/default/20250521/255d4770ef7d36973bd1cbf0f6af21f4.png',
    class: 'hot'
  },
	new: {
    img: '/storage/default/20250521/bc708f1466fb66ddccda244c654ed05d.png',
    class: 'new'
  },
	good: {
    img: '/storage/default/20250521/be563dd7086007564c346902a680bd73.png',
    class: 'good'
  },
	day7: {
    img: '/storage/default/20250521/ebfa4ca1ab7a128de47e574a5a7a6945.png',
    class: 'day7'
  }
}
/**
 * @description 根据商品信息推断标签
 * @param {*} goods 
 * @param 七天无理由每一条数据都展示
 * @param {Number} goods.sales 销量大于10 => 热卖
 * @param {String} goods.create_time 创建时间在30天以内=> 新品
 * @param 优选好物，默认返回
 * @returns 
 */
export function tagInferShop(goods){
  const tags = [];

	if(goods?.sales > 10){ // 热卖 
    tags.push(tagEnum1.hot);
	} 
  if(isWithin30Days(goods.create_time.split(' ')[0])){ // 创建时间在30天以内=> 新品
    tags.push(tagEnum1.new);
	}

  // 3. 添加优选好物标签
  tags.push(tagEnum1.good);
   
  // 4. 如果标签数量不足3个，添加7天无理由
  if (tags.length < 3) {
    tags.push(tagEnum1.day7);
  }

  
	return tags.slice(0, 3);
}
export function tagInfer(goods){
	if(goods.price[0] < 10){
		return tagEnum.xstj
	}else if(goods?.sales > 10){ // 
		return tagEnum.whbk
	}else if(isWithin30Days(goods.create_time.split(' ')[0])){ // 创建时间在30天以内=> 新品
		return tagEnum.xpss
	}
	return tagEnum.yxhw
}
function isWithin30Days(dateStr) {
  const inputTime = new Date(dateStr);
  const now = new Date();

  if (isNaN(inputTime.getTime())) {
	throw new Error('无效的日期格式');
  }
  const diffMs = now - inputTime;
  const diffDays = diffMs / (1000 * 60 * 60 * 24);
  return diffDays <= 30;
}